
{    
Indicator:         _TickRate.OOEL

Description:       Calculates the average tick rate for A symbol over a period of Length seconds.

                   This indicator uses a Timer component to execute a handler method every 1 second.

                   The following items were created or modified:
                       Component tray: Timer1 dragged into document from the Toolbox
                       Properties editor: Interval property set to 1000 milliseconds (1 second)
                       Properites editor: Enable property set to True to start the timer when it is applied to a chart or RadarScreen.
                       Properties editor: Update event assigned to the EasyLanguage method below.
                       
                   NOTE: AutoReset defaults to True indicating that the countdown automatically repeats after each timeup event.

Author:            MarkSanDiego

Updated:           02/13/11    original version
                   03/14/11    TS Forum release
}

inputs:
    Length(60),             { number of seconds over which tick rate will be averaged - must be between 1 and 300 seconds }
    Mult(60),               { multiplier to express results in other than ticks per second.  For ex.: use Mult = 60 for ticks per min }
                            { or, Mult = close  for dollars traded per second instead of ticks per second }
    CalcSeconds(1);         { Number of seconds between TickRate display updates }                                                    
                                                
    
vars:
    Intrabarpersist Seconds(0),                              { number of elapsed Seconds indicator in use }
    Intrabarpersist ArrayLength(0),                          { number of elements in circular array (each element represents 1 second) }
    Intrabarpersist index(0),                                { circular Array index }
    intrabarpersist TickRate(0),                             { number of ticks per second averaged over Period seconds }
    intrabarpersist int TickSec(0),                          { total sum of ticks in 1 second }
    Intrabarpersist TickTotal(0),                            { total sum of ticks in Length seconds }
    Intrabarpersist bool LastBar(false),                     { true when LastBarOnChart is reached }
    intrabarpersist bool RealTime(false),                    { indicates if real time trading is active }
    Intrabarpersist bool DoCalc(false),
    bool RadarScreen(GetAppInfo(aiApplicationType) = 2);     { true if indicator applied to RadarScreen, false if applied to chart }                 

arrays:
    intrabarpersist CountArray[300](0);    


{ Components declaration (Designer generated code) }
    var: elsystem.Timer Timer1( NULL ),elsystem.Timer Timer2( NULL );

    { This method gets called by EasyLanguage one time 
      at the beginning to create and initialize the components }
    method override void InitializeComponent()
    begin
        Timer1 = new elsystem.Timer;
        timer1.Interval = 1000;
        timer1.AutoReset = true;
        timer1.Enable = true;
        timer1.Name = "timer1";
        Timer1.Elapsed += Timer1_Elapsed;  
    end;


    method void Timer1_Elapsed( elsystem.Object sender, elsystem.TimerElapsedEventArgs args ) 
    {The Elapsed event handler is called when the timer counts down to zero}
    begin
  
        Seconds = Seconds + 1; 
        
        { adjust length of circular array until array fully loaded }
        If ArrayLength < Length then ArrayLength = ArrayLength + 1;

        DoCalc = Mod(Seconds, CalcSeconds) = 0;
        
        index = index + 1;                                   { increment index each second } 
        If Index > Length then Index = 1;                    { create circular array index }
        
        TickTotal = TickTotal - CountArray[index] + TickSec;
        CountArray[index] = TickSec;
        TickSec = 0;
         
        If DoCalc then begin
        
            TickRate = TickTotal/ArrayLength * Mult;
            
            If RadarScreen then begin
                plot1(Length, "Period (sec)");
                plot2(Mult, "Mult (sec)");
                plot3(CalcSeconds, "Calc (sec)");
            end;
                            
            if TickRate > 0 then plot4(TickRate,"Tick Rate");
    
        end;
        
    end;


    Once (GetAppInfo(aiRealTimeCalc) = 1) begin
        RealTime = true;
    end;  
    
    Value1 = ticks;                                           { force update with every tick }
    if RealTime then TickSec = TickSec + 1;
     

    
